package com.ruoyi.controller.masterdate;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.jpjs.param.ParamService;
import com.ruoyi.service.jpjs.person.PersonService;
import com.ruoyi.service.masterdate.DepartmentService;
import com.ruoyi.service.masterdate.EmployeeService;
import com.ruoyi.service.masterdate.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController {

    private String prefix = "masterdate/employee";

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private PostService postService;

    @GetMapping()
    public String person() {
        return prefix + "/Employee";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findEmployeeAll(@RequestParam Map<String, Object> map) {
        List<Map<String, Object>> list = employeeService.findEmployeeAll(map);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String addEmployee(ModelMap mmp) {
        mmp.put("reportTo",employeeService.findEmployeeAll(null));
        mmp.put("department",departmentService.findDepartmentAll(null));
        mmp.put("post",postService.findPostAll(null));
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addEmployee(@RequestParam Map<String, Object> map) {
        return toAjax(employeeService.addEmployee(map));
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editEmployee(@RequestParam Map<String, Object> map) {
        return toAjax(employeeService.editEmployee(map));
    }

    @GetMapping("/edit/{id}")
    public String editEmployee(@PathVariable("id") String id, ModelMap mmp) {
        mmp.put("reportTo",employeeService.findEmployeeAll(null));
        mmp.put("department",departmentService.findDepartmentAll(null));
        mmp.put("post",postService.findPostAll(null));
        mmp.put("employee", employeeService.findEmployee(id));
        return prefix + "/edit";
    }

    @PostMapping("/checkNumberUnique")
    @ResponseBody
    public int checkNumberUnique(@RequestParam Map<String, Object> map) {
        return employeeService.checkNumberUnique(map);
    }

}
